unit TPS_PindahOrang;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Setting, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection, ExtCtrls;

type
  TForm_PindahOrang = class(TForm)
    lblNIK: TLabel;
    lblNama: TLabel;
    lblTPS: TLabel;
    Label4: TLabel;
    cbTPS: TComboBox;
    Label5: TLabel;
    lblAlamat: TLabel;
    lblKet: TLabel;
    btnPindah: TButton;
    btnBatal: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    cbDesa: TComboBox;
    Panel1: TPanel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnBatalClick(Sender: TObject);
    procedure btnPindahClick(Sender: TObject);
    procedure cbTPSChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; ptabel:String; idu:String; idtps:String; desactive:String;pemilihs:Tstringlist;kodes:Tstringlist);             overload;
  end;

var
  Form_PindahOrang: TForm_PindahOrang;
  tps,tps2,pid,sdesa,skec,desa_tps:String;
  ct:Integer;
  list_pemilih, list_kode:tstringlist;

implementation

{$R *.dfm}

constructor TForm_PindahOrang.Create(AOwner: TComponent; ptabel:String; idu:String; idtps:String; desactive: String; pemilihs:TStringList; kodes:TStringList);
begin
  list_kode:=kodes;
  list_pemilih:=pemilihs;
  tps:=idtps;
  pid:=idu;
  sdesa:=desactive;
  desa_tps:=ptabel;
  skec:=copy(ptabel,1,5);
  inherited Create(AOwner);
end;

procedure TForm_PindahOrang.btnBatalClick(Sender: TObject);
begin
  Close;
end;

procedure TForm_PindahOrang.btnPindahClick(Sender: TObject);
var
  kodpem,koddes:String;
  pos, total, iter:Integer;
  tanggal,idtgl:string;
begin
  //masukin tanggal dulu gan
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
    ExecSQL;
  end;
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
    Active:=True;
    idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
  end;
  total := list_pemilih.Count;
  if ((ct+total)<=600) then begin
    for iter:=0 to total-1 do begin
      kodpem:=list_pemilih[iter];
      koddes:=list_kode[iter];
      with ZQuery1 do
      begin
        Active:=False;
        sql.Clear;
        sql.Text:='UPDATE pemilih_'+koddes+' SET TPS='+tps2+', ID_TANGGAL = '+idtgl+' WHERE ID_USER='+QuotedStr(kodpem);
//      showmessage('update'+sdesa+', '+tps2+', '+kodpem);
        ExecSQL;
      end;
    end;
    ShowMessage('Pemilih berhasil dipindahkan');
    Close;
  end
    else
  ShowMessage('TPS sudah penuh, pemilih tidak berhasil dipindahkan');
end;

procedure TForm_PindahOrang.cbTPSChange(Sender: TObject);
var
  query:String;
begin
  if (cbTPS.ItemIndex > 0) then
  begin
    tps2:=cbTPS.Items[cbTPS.ItemIndex];
    //ShowMessage(tps2);
    with ZTable1 do begin
      active:=false;
      TableName:='daftar_tps';
      filter:='NAMA='+QuotedStr(tps2)+' AND KODE_DESA = '+quotedstr(desa_tps);
      filtered:=true;
      Active:=true;
      first;
      lblAlamat.Caption:=FieldByName('ALAMAT').AsString;
      tps2:=FieldByName('ID_TPS').AsString;
    end;
    query:='TPS='+tps2;
    ZQuery1.Active := False;
    query:= 'SELECT COUNT(*) FROM pemilih_'+desa_tps+' WHERE '+query;
    ZQuery1.SQL.Text := query;
    ZQuery1.Active := True;
    ZQuery1.First;  //tabel simpan
    ct:=ZQuery1.Fields[0].AsInteger;
    btnPindah.Enabled:=true;
  end
    else
  btnPindah.Enabled:=false;
end;

procedure TForm_PindahOrang.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm_PindahOrang.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
  kdesa:String;
  indx, total:integer;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  ZQuery1.Connection:=ZConnection1;
  ZTable1.Connection:=ZConnection1;
  BorderIcons := BorderIcons - [biMaximize];
  //showmessage (sdesa);

  total := list_pemilih.Count;
  //panel1.Caption := 'Memindahkan '+inttostr(total)+' pemilih';
  //TPS dan Keterangan
  with ZTable1 do begin
    active:=false;
    TableName:='daftar_tps';
    filter:='ID_TPS='+tps;
    filtered:=true;
    Active:=true;
    first;
    lblTPS.Caption:=lblTPS.Caption+FieldByName('NAMA').AsString;
    panel1.Caption := 'Memindahkan '+inttostr(total)+' pemilih dari TPS '+FieldByName('NAMA').AsString;
    kdesa:=FieldByName('KODE_DESA').AsString;
  end;
  {with ZTable1 do begin
    active:=false;
    TableName:='kabkot';
    filter:='KODE='+Copy(kdesa,1,2);
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=fieldbyname('TAMPIL').AsString+', ';
  end;
  with ZTable1 do begin
    active:=false;
    TableName:='kecamatan';
    filter:='KODE_KECAMATAN='+Copy(kdesa,3,3);
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=lblKet.Caption+fieldbyname('TAMPIL').AsString+' : ';
  end;
  with ZTable1 do begin
    active:=false;
    TableName:='desa';
    filter:='KODE_DESA='+kdesa;
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=lblKet.Caption+fieldbyname('TAMPIL').AsString;
  end;
  //NIK dan Nama
  with ZTable1 do
  begin
    Active:=False;
    TableName:='pemilih_'+sdesa;
    Filter:='ID_USER = '+pid;
    Filtered:=True;
    Active:=True;
    First;
  end;
  lblNIK.Caption := lblNIK.Caption+ZTable1.FieldByName('NIK').AsString;
  lblNama.Caption := lblNama.Caption+ZTable1.FieldByName('NAMA').AsString;}
  // dataTPS
  cbTPS.Clear;
  cbTPS.Items.Add('Pilih TPS...');
  with ZTable1 do
  begin
    active:=false;
    tablename:='daftar_tps';
    filter:='KODE_DESA='+QuotedStr(desa_tps);
    filtered:=true;
    active:=true;
    first;
    while (not eof) do
    begin
//      showmessage(FieldByName('ID_TPS').AsString);
      if(FieldByName('ID_TPS').AsString<>tps)then
      cbTPS.Items.Add(FieldByName('NAMA').AsString);
      next;
    end;
  end;
  cbDesa.clear;
  cbDesa.Items.Add('Pilih Desa...');
  indx := 0;
  with ZTable1 do
  begin
    active:=false;
    tablename:='desa';
    filter:='KODE_KECAMATAN='+QuotedStr(skec);
    filtered:=true;
    active:=true;
    first;
    //showmessage(desa_tps);
    while (not eof) do
    begin
      indx:=indx+1;
      cbDesa.Items.Add(FieldByName('TAMPIL').AsString);
      if(FieldByName('KODE_DESA').AsString=desa_tps)then
        cbDesa.ItemIndex := indx;
      next;
    end;
  end;
  btnPindah.Enabled:=false;
end;

procedure TForm_PindahOrang.cbDesaChange(Sender: TObject);
var
  textdesa:string;
begin
  if(cbDesa.ItemIndex > 0) then begin
    textdesa := cbDesa.Items[cbDesa.itemIndex];
    with ZTable1 do begin
      active:=false;
      TableName:='desa';
      filter:='TAMPIL='+QuotedStr(textdesa)+' AND KODE_KECAMATAN = '+quotedstr(skec);
      filtered:=true;
      Active:=true;
      first;
      //lblAlamat.Caption:=FieldByName('ALAMAT').AsString;
      desa_tps:=FieldByName('KODE_DESA').AsString;
    end;
    with ZTable1 do begin
      active:=false;
      TableName:='daftar_tps';
      filter:='KODE_DESA = '+quotedStr(desa_tps);
      filtered:=true;
      Active:=true;
      first;
      cbTPS.Clear;
      cbTPS.Items.Add('Pilih TPS..');
      while not eof do begin
        if(tps <> fieldByName('ID_TPS').asString) then
          cbTPS.Items.Add(fieldByName('NAMA').AsString);
        next;
      end;
    end;
  end
  else
    cbTPS.Clear;
end;

end.
